package service;

import android.database.sqlite.SQLiteDatabase;

import java.util.Objects;

import com.example.firstapp.entity.Sign;

import util.Database;
import util.MD5Utils;

/**
 * 注册业务逻辑
 */
public class SignService {

    /**
     * 验证并注册
     *
     * @param sign 注册信息实体对象
     */
    public static void sign(SQLiteDatabase db, Sign sign) {
        if (Objects.equals(sign.getName().trim(), "")) {
            sign.setState(false);
            sign.setMessage("请输入用户名！");
        } else if (sign.getPhotoNumber().trim().length() != 11) {
            sign.setState(false);
            sign.setMessage("手机号输入有误！");
        } else if (Objects.equals(sign.getPassword().trim(), "")) {
            sign.setState(false);
            sign.setMessage("请输入密码！");
        } else if (Objects.equals(sign.getEnPassword().trim(), "")) {
            sign.setState(false);
            sign.setMessage("请确认密码！");
        } else if (!Objects.equals(sign.getEnPassword(), sign.getPassword())) {
            sign.setState(false);
            sign.setMessage("输入密码不一致");
        } else {
            String[][] data = {{"name", sign.getName()},
                    {"password", MD5Utils.md5(sign.getPassword())},
                    {"photoNumber", sign.getPhotoNumber()}};
            if (Database.addDate(db, "user", data)) {
                sign.setState(true);
                sign.setMessage("注册成功,请返回登录！");
            } else {
                sign.setState(false);
                sign.setMessage("该用户已存在！");
            }
        }
    }
}
